package com.ce.wnd;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import com.ce.po.VFilmDO;
import com.ce.po.VSeriesDO;
import com.ce.po.VSetDO;
import com.ce.po.VTagDO;
import com.ce.po.VTypeDO;
import com.ce.thd.WriteRunnable;
import com.ce.utils.DownloadRes;
import com.ce.utils.EtvNetWork;
import com.ce.utils.FileOperate;
import com.ce.utils.FileResDownload;
import com.ce.utils.NWTransform;

public class TestMain {
	public static void main(String[] args) {
		try {
			//testGetData();
			//testThread();
			//testDownloadFile();
			//testThreadDownloadFile();
			Map<String, String> map = new HashMap<String, String>();
//			map.put("test1", "v1");
//			map.put("test2", "v2");
//			map.put("test3", "v3");
			Set<String> keys = map.keySet();
//			System.out.println(keys.toArray());
//			map.remove("test1");
//			System.out.println(keys.toArray());
			String tmp = null;
			map.put(tmp, "null");
			map.put(tmp, "null");
			map.put(tmp, "null");
			System.out.println(keys.toArray());
			for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
				String string = (String) iterator.next();
				System.out.println(string);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void testThreadDownloadFile() throws Exception {
		Stack<String> stock = new Stack<String>();
		Thread tc1 = new Thread(new DownloadRes(stock, "thread-1"));
		Thread tc2 = new Thread(new DownloadRes(stock, "thread-2"));
		Thread tc3 = new Thread(new DownloadRes(stock, "thread-3"));
		tc1.start();
		tc2.start();
		tc3.start();
		String channel = "4";
		String tagsJson = EtvNetWork.getTags(channel);
		List<VTagDO> tags = NWTransform.transformJsonToTags(tagsJson);
		for (VTagDO tagDO : tags) {
			System.out.println(tagDO);
			for (VTypeDO typeDO : tagDO.getTypes()) {
				for (int i = 1; i <= 1; i++) {
					String page = ""+i;
					String type = typeDO.getId();
					String listJson = EtvNetWork.getList(channel, type, page);
					List<VFilmDO> films = NWTransform.transformJsonToList(listJson);
					for (VFilmDO filmDO : films) {
						String detailJson = EtvNetWork.getDetail(filmDO.getId(), filmDO.getType());
						VSeriesDO seriesDO = NWTransform.transformJsonToVSeriesDO(detailJson);
						System.out.println(filmDO.getPic());
						//FileResDownload.downloadPic(filmDO.getPic());
						stock.add(filmDO.getPic());
						if(seriesDO == null)
							seriesDO = NWTransform.transformJsonToVSeriesDO(detailJson);
						if(seriesDO == null)
							break;
						List<VSetDO> sets = seriesDO.getSets();
						if(sets != null && sets.size()>0) {
							for (VSetDO setDO : sets) {
								if(setDO.getVideoType().equals("2")) {
									System.out.println(setDO.getSource());
									//FileResDownload.downloadSwf(setDO.getSource());
									//stock.add(setDO.getSource());
									stock.push(setDO.getSource());
								}
							}
						}
					}
				}
			}
		}
	}
	
	private static void testDownloadFile() throws Exception {
		String channel = "4";
		String tagsJson = EtvNetWork.getTags(channel);
		List<VTagDO> tags = NWTransform.transformJsonToTags(tagsJson);
		for (VTagDO tagDO : tags) {
			System.out.println(tagDO);
			for (VTypeDO typeDO : tagDO.getTypes()) {
				for (int i = 1; i <= 1; i++) {
					String page = ""+i;
					String type = typeDO.getId();
					String listJson = EtvNetWork.getList(channel, type, page);
					List<VFilmDO> films = NWTransform.transformJsonToList(listJson);
					for (VFilmDO filmDO : films) {
						String detailJson = EtvNetWork.getDetail(filmDO.getId(), filmDO.getType());
						VSeriesDO seriesDO = NWTransform.transformJsonToVSeriesDO(detailJson);
						System.out.println(filmDO.getPic());
						//FileResDownload.downloadPic(filmDO.getPic());
						List<VSetDO> sets = seriesDO.getSets();
						if(sets != null && sets.size()>0) {
							for (VSetDO setDO : sets) {
								if(setDO.getVideoType().equals("2")) {
									System.out.println(setDO.getSource());
									FileResDownload.downloadSwf(setDO.getSource());
								}
							}
						}
					}
				}
			}
		}
	}
	private static void testThread() throws Exception {
		String[] channels = new String[] {"4"};
		Thread th =  new Thread(new WriteRunnable(channels));
		th.start();
	}
	private static void testGetData() throws Exception {
		FileOperate fileOperate = new FileOperate("E:\\wypsmall\\Code\\Code_j2ee_e34\\NewEtvSDExport\\exp.properties");
		fileOperate.prepare();
		String channel = "4";
		String tagsJson = EtvNetWork.getTags(channel);
		fileOperate.writeTagProperties(tagsJson, channel);
		List<VTagDO> tags = NWTransform.transformJsonToTags(tagsJson);
		for (VTagDO tagDO : tags) {
			System.out.println(tagDO);
			for (VTypeDO typeDO : tagDO.getTypes()) {
				for (int i = 1; i <= 1; i++) {
					String page = ""+i;
					String type = typeDO.getId();
					String listJson = EtvNetWork.getList(channel, type, page);
					fileOperate.writeListProperties(listJson, channel, type, page);
					List<VFilmDO> films = NWTransform.transformJsonToList(listJson);
					for (VFilmDO filmDO : films) {
						String detailJson = EtvNetWork.getDetail(filmDO.getId(), filmDO.getType());
						fileOperate.writeDetailProperties(detailJson, filmDO.getId(), filmDO.getType());
						VSeriesDO seriesDO = NWTransform.transformJsonToVSeriesDO(detailJson);
						//System.out.println(seriesDO.getSets().get(0).getSource());
					}
					fileOperate.flush();
				}
				fileOperate.flush();
			}
		}
		fileOperate.finish();
	}
}
